<?php
/* explain:		news system
 * Project:     
 * File:        news.php
 *
 * @ link 		http://www.../modules/
 * @ Email		ldmmyx@hotmail.com
 * @ copyright 	2005  Ling Deming
 * @ author 	ivan.ling  Ling Deming
 * @ version 	1.0
 *
 */
include_once("utils/CommonUtils.php");
include_once("domain/PromotionTypeDomain.php");
include_once("dao/CommonDAO.php");

class PromotionTypeImpl extends CommonUtils implements CommonDAO
{

	function __construct($DBTag)
	{
		parent::__construct();
		
		$this->DBTag = $DBTag;
	}

	/* get data via ID
	 *
	 * @param  int    id
	 * @return mixed  false:
	 */
	public function getById($id)
	{
		$sql  = "SELECT id, name, descript, status FROM promotion_type WHERE id = $id";
		
		$conn 	= null;
		$obj	= null;
		
		try {
			$conn 	= $this->getConnection($this->DBTag);
			$rs 	= mysql_query($sql, $conn);
			if ($row = mysql_fetch_row($rs)) {
				$obj = new PromotionTypeDomain();

				$obj->setId($row[0]);
				$obj->setName($row[1]);
				$obj->setDescript($row[2]);
				$obj->setStatus($row[3]);
				
			} else {
				$this->messages = "get data failed by Id. sql : $sql <br>";
				$this->messages .= "error: " . mysql_error($conn);
			}
		} catch (Exception $e) {
			$this->messages = $e->getMessage();
		}
		
		$this->freeConnection($this->DBTag, $conn);
		return $obj;
	}

	public function add($obj)
	{		
		if($obj instanceof PromotionTypeDomain){			
			$sql 	 = "INSERT INTO promotion_type SET createdTime = UTC_TIMESTAMP(), descript='";
			$sql	.= addslashes($obj->getDescript());
			
			$sql 	.= "', name='";
			$sql	.= addslashes($obj->getName());
	
			$sql 	.= "', descript='";
			$sql	.= addslashes($obj->getDescript());
	
			$sql	.= "', status=";
			$sql	.= $obj->getStatus();
			
			$id = $this->insert($this->DBTag, $sql);
			
			$obj->setId($id);			
		}else{
			$id	= -1;
			
			$this->message = 'input object is not instance of PromotionTypeDomain.';
		}
		
		return $id;
	}
	
	public function edit($obj)
	{		
		if($obj instanceof PromotionTypeDomain){			
			$sql 	 = "UPDATE promotion_type SET descript='";
			$sql	.= addslashes($obj->getDescript());
			
			$sql 	.= "', name='";
			$sql	.= addslashes($obj->getName());
	
			$sql	.= "', status=";
			$sql	.= $obj->getStatus();
			
			$sql	.= " WHERE id = ";
			$sql	.= $obj->id();			
			
			return $this->update($this->DBTag, $sql);			
		}else{
			$id	= -1;
			
			$this->message = 'input object is not instance of PromotionTypeDomain.';
		}
		
		return false;
	}
	
	/* delete news class
	 *
	 * @param  int    	id
	 * @param  string  	status
	 * @param  return 	boolean
	 */
	public function delete($id)
	{
		$sql = "DELETE FROM promotion_type WHERE id = $id";
		
		return $this->update($this->DBTag, $sql);
	}
	
	public function getCoupons($userId, $order = 'ORDER BY id DESC', $page = 0, $discount = 20)
	{
		return $this->getCustoms(" WHERE userId = $userId", $order, $page, $discount);
	}
	
	/* list
	 *
	 * @ param  string 	class id
	 * @ param  string 	page
	 * @ param  string 	discount
	 * @ param  string 	url
	 * @ param  return 	array
	 */
	public function getCustoms($where, $order, $page = 0, $discount = 20)
	{
		$arrback 	= array();
		$conn 		= null;
		
		try{
			$conn 	= $this->getConnection($this->DBTag);
			
			$sql 	= "SELECT count(1) FROM promotion_type $where";	
			$rs 	= mysql_query($sql, $conn);// or die("<br><br>Invalid query: " . $sql . mysql_error($conn));
			
			list($total) = mysql_fetch_row($rs);
			
			$this->mutilPager($total, $page, $discount);
			
			if(!empty($total)){
				$sql  = 'SELECT id, name, descript, status, modifiedTime ';
				$sql .= " FROM promotion_type $where $order";
				$sql .= " LIMIT $this->firstcount, $discount";
				
				$rs  = mysql_query($sql, $conn);
				if($rs){
					while($row = mysql_fetch_array($rs)){
						$obj = new PromotionTypeDomain();

						$obj->setId($row[0]);
						$obj->setName($row[1]);
						$obj->setDescript($row[2]);
						$obj->setEndDate($row[3]);
						
						$arrback[] = $obj;
					}
				}else {
					$this->message = "select failed. sql : $sql";
					$this->message .= "<br>error: " . mysql_error($conn);
				}
			}
		}catch(Exception $e){
			$this->message = $e->getMessage();			
		}
		
		$this->freeConnection($this->DBTag, $conn);
		return $arrback;
	}
	

	public function getSelect()
	{
		$sql = "SELECT id, name FROM promotion_type WHERE status = 1 ORDER BY name";
		
		return $this->getHash($this->DBTag, $sql);
	}

	
	public function getMessage() 
	{
		return $this->message;
	}

	
	
}
?>
